package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.TypeParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TdSysTypeService;
import com.onluy.sysman.entity.sysmgr.TdSysType;

@Validated
@RestController
@RequestMapping("/type")
@Api(value="type", description="/通用类型管理接口", position=1 )
public class TdSysTypeController extends AppWalkController {


	//调用服务
	@Autowired
	private TdSysTypeService tdSysTypeService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据 areaCode,sysCode,typeCode  获取信息")
	public RspInfo<TdSysType> dotarget(@Valid @RequestBody ReqInfo<ReqHead, TypeParam> reqInfo   ) throws Exception {
		RspInfo<TdSysType> jsonResult = new RspInfo<TdSysType>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TdSysType tdSysType = new TdSysType();


		tdSysType.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysType.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysType.setTypeCode(reqInfo.getReqBody().getTypeCode()).asCondition();
		
		
		tdSysType = tdSysTypeService.model(tdSysType);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tdSysType);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TdSysType> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TdSysType tdSysType = reqInfo.getReqBody();
		num = tdSysTypeService.add(tdSysType);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据  areaCode,sysCode,typeCode 更新")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TdSysType> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;  
		TdSysType tdSysType = reqInfo.getReqBody();


		tdSysType.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysType.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysType.setTypeCode(reqInfo.getReqBody().getTypeCode()).asCondition();
		
		num=tdSysTypeService.update( tdSysType);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据   areaCode,sysCode,typeCode  删除")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,TypeParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TdSysType tdSysType = new TdSysType();
		int num=1; 

		tdSysType.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysType.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysType.setTypeCode(reqInfo.getReqBody().getTypeCode()).asCondition();
		
			num=tdSysTypeService.del( tdSysType);
		 
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="列表明细")
	public RspInfo<PageData<TdSysType>>  doList(@RequestBody    ReqInfo<ParamPagination, TdSysType> reqInfo )throws Exception {
		RspInfo<PageData<TdSysType>> jsonResult=new RspInfo<PageData<TdSysType>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件

		PageData<TdSysType> list =tdSysTypeService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TdSysType tdSysType){

		if(tdSysType == null )return;

		if(null!=tdSysType.getAreaCode()){
			tdSysType.setAreaCode(tdSysType.getAreaCode()).asCondition();
		}if(null!=tdSysType.getSysCode()){
			tdSysType.setSysCode(tdSysType.getSysCode()).asCondition();
		}if(null!=tdSysType.getTypeCode()){
			tdSysType.setTypeCode(tdSysType.getTypeCode()).asCondition();
		}
		if(null!=tdSysType.getParentTypeCode()){
			tdSysType.setParentTypeCode(tdSysType.getParentTypeCode()).asCondition();
		}
		if(null!=tdSysType.getTypeName()){
			tdSysType.setTypeName(null).getCondition().andLike( "%"+tdSysType.getTypeName()+"%" );
		}

		if(null!=tdSysType.getTypeDesc()){
			tdSysType.setTypeDesc(null).getCondition().andLike( "%"+tdSysType.getTypeDesc()+"%" );
		}

	}


}
